﻿---@diagnostic disable: trailing-space
local extension = Package:new("moepack")
extension.extensionName = "moepack"

local U = require "packages.utility.utility"

local MoeFunc = require "packages.moepack.moepack_function"













local emo__junwang = fk.CreateTriggerSkill{
  name = "emo__junwang",
  events = {fk.BeforeGameOverJudge},
  frequency = Skill.Limited,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and data.damage and data.damage.from == player
    and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
    and table.find(player.room.alive_players, function (p) return p.role == "renegade" end)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = room:askForChoice(player, {"loyalist", "rebel"}, self.name)
    for _, p in ipairs(room.alive_players) do
      if p.role == "renegade" then
        room:setPlayerProperty(p, "role", choice)
        --room:setPlayerProperty(p, "role_shown", true)
      end
    end
  end,
}
moe__rosalyn:addSkill(emo__junwang)

Fk:loadTranslationTable{
  ["moe__rosalyn"] = "罗莎琳",
  ["#moe__rosalyn"] = "血色宴会",
  
  ["emo__junwang"] = "君望",
  [":emo__junwang"] = "限定技，你杀死一名角色时，你可以将存活的内奸的身份改为反贼或忠臣。",
  ["#emo__junwang_choose"] = "你须对至少一名其他角色发动“君望”",
  ["#emo__junwang_losehp"] = "令 %src 失去1点体力",
  ["#emo__junwang_drawcard"] = "令 %src 摸两张牌",

  ["$emo__junwang1"] = "我亦可为王",
  ["$emo__junwang2"] = "窃钩者诛，窃国者侯",
}



local moe__xiangyan = General(extension, "moe__xiangyan", "moe", 3, 3, General.Female)

local emo__yanjiaos = fk.CreateActiveSkill{
  name = "emo__yanjiaos",
  anim_type = "support",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_num = 3,
  card_filter = function(self, to_select, selected)
    return #selected < 3 and table.contains(Self.player_cards[Player.Hand], to_select)
  end,
  target_num = 1,
  target_filter = function(self, to_select, selected, cards)
    return #selected == 0 and to_select ~= Self.id and #cards == 3
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    player:showCards(effect.cards)
    local Ocards1 = effect.cards
    local Ocards2 = room:getNCards(3)
    local to_get = room:askForCardsChosen(target, player, 3, 3, { card_data = { {player.general, Ocards1},{"Top", Ocards2} } }, self.name, "#emo__yanjiaos-choose:"..player.id)
    local get_fromHand,get_fromTop,to_back = {},{},{} 
    table.insertTable(Ocards1, Ocards2)
    for _,cid in ipairs(to_get) do
      if table.contains(player:getCardIds("h"), cid) then
        table.insert(get_fromHand, cid)
      end
    end
    for _,cid in ipairs(Ocards2) do
      if table.contains(to_get, cid) then
        table.insert(get_fromTop, cid)
      else
        table.insert(to_back, cid)
      end
    end
    local moveInfos = {}
    if #get_fromHand > 0 then -- 其获得你的牌
      table.insert(moveInfos,{
        to = target.id,
        ids = get_fromHand,
        from = player.id,
        toArea = Player.Hand,
        moveReason = fk.ReasonGive,
        proposer = player.id,
        skillName = self.name,
      })
    end
    if #get_fromTop > 0 then -- 其获得牌堆牌
      table.insert(moveInfos, {
        to = target.id,
        ids = get_fromTop,
        toArea = Player.Hand,
        moveReason = fk.ReasonPrey,
        skillName = self.name,
      })
    end
    if #to_back > 0 then
      table.insert(moveInfos, {-- 自己获得
        to = player.id,
        ids = to_back,
        toArea = Player.Hand,
        moveReason = fk.ReasonPrey,
        proposer = player.id,
        skillName = self.name,
      })
    end
    room:moveCards(table.unpack(moveInfos))
    if player.dead then return end
    if #get_fromHand <= 1 then
      room:changeMaxHp(player, 1)
    elseif not player:isNude() then
      -- AI 用这个视为技会报错
      local _, dat = room:askForUseViewAsSkill(player, "emo__yanjiaos_viewas", "#emo__yanjiaos_viewascard", true, {bypass_times = true})
      if dat then
        local card = Fk.skills["emo__yanjiaos_viewas"]:viewAs(dat.cards)
        local mark = player:getTableMark("emo__yanjiaos")
        table.insert(mark, card.name)
        room:setPlayerMark(player, "emo__yanjiaos", mark)
        room:useCard{
          from = player.id,
          tos = table.map(dat.targets, function(p) return {p} end),
          card = card,
          extraUse = true,
        }
      end
    end
  end,
}

local emo__yanjiaos_viewas = fk.CreateViewAsSkill{
  name = "emo__yanjiaos_viewas",
  interaction = function()
    local names, all_names = {}, {}
    for _, id in ipairs(Fk:getAllCardIds()) do
      local card = Fk:getCardById(id)
      if card.type == Card.TypeTrick and not card.is_derived and Self:canUse(card) then
        table.insertIfNeed(all_names, card.name)
        if not table.contains(Self:getTableMark("emo__yanjiaos"), card.name) then
          table.insertIfNeed(names, card.name)
        end
      end
    end
    if #names == 0 then return end
    return UI.ComboBox {choices = names, all_choices = all_names}
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  view_as = function(self, cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcard(cards[1])
    card.skillName = "emo__yanjiaos"
    return card
  end,
}
Fk:addSkill(emo__yanjiaos_viewas)

moe__xiangyan:addSkill(emo__yanjiaos)

local emo__ximing = fk.CreateTriggerSkill{
  name = "emo__ximing",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and (not target.dead) and not target:isNude()
    and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    return room:askForSkillInvoke(player, self.name, nil, "#emo__ximing-invoke:"..target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:askForDiscard(target, 1, 1, true, self.name, false, ".", "#emo__ximing-discard")
    if target.dead then return end
    if #cards > 0 then
      local card = Fk:getCardById(cards[1])
      if card.color == Card.Red and card.type == Card.TypeBasic then
        room:recover { num = 1, skillName = self.name, who = target, recoverBy = player}
      else
        local x = target.maxHp - target:getHandcardNum()
        if x > 0 then 
          target:drawCards(x, self.name) 
        end
      end
    end
  end,
}
moe__xiangyan:addSkill(emo__ximing)

Fk:loadTranslationTable{
  ["moe__xiangyan"] = "湘燕",

  ["emo__yanjiaos"] = "言教",
  [":emo__yanjiaos"] = "出牌阶段限一次，你可以展示三张手牌，令一名其他角色观看牌堆顶三张牌，其在这六张牌中选择三张获得，你获得其余牌。若其获得你的牌数："..
  "<br>①不大于1，你增加1点体力上限。"..
  "<br>②大于1，你可以将一张牌当任意锦囊牌使用（每种牌名限一次）。",

  ["#emo__yanjiaos-choose"] = "言教：选择三张获得，其余牌令 %src 获得",
  ["DrawPile"] = "牌堆",
  ["#emo__yanjiaos-target"] = "言教：可将选择的牌交给一名角色，点“取消”交给自己",
  ["emo__yanjiaos_viewas"] = "言教",
  ["#emo__yanjiaos_viewascard"] = "言教：你可以将一张牌当任意锦囊牌使用",

  ["emo__ximing"] = "惜命",
  [":emo__ximing"] = "每回合一次，每当一名角色受到伤害后，你可以令其弃置一张牌：若弃置了红色基本牌，其回复1点体力；若为其他牌，其将手牌摸至体力上限。",
  ["#emo__ximing-invoke"] = "是否对 %src 发动“惜命”",
  ["#emo__ximing-discard"] = "惜命：请弃置一张牌，若为红色基本牌，回复1点体力，否则你将手牌摸至体力上限",
}

local moe__qianxueji = General(extension, "moe__qianxueji", "moe", 4, 4, General.Female)

local emo__mingdao = fk.CreateTriggerSkill{
  name = "emo__mingdao",
  events = {fk.GameStart , fk.AfterCardsMove},
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if event == fk.GameStart then
      return player:hasSkill(self)
    else
      if player:hasSkill(self) and player.phase == Player.Play then
        local x = 0
        for _, move in ipairs(data) do
          if move.toArea == Card.DiscardPile then
            for _, info in ipairs(move.moveInfo) do
              local card = Fk:getCardById(info.cardId)
              if card.type == Card.TypeEquip then
                x = x + 1
              end
            end
          end
        end
        if x > 0 then
          self.cost_data = x
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      local cardId = MoeFunc.getDerivedCard(room, "nameless_sword")
      if cardId and room:getCardArea(cardId) == Card.Void then
        table.removeOne(room.void, cardId)
        table.insert(room.draw_pile, math.random(1, #room.draw_pile), cardId)
        room:setCardArea(cardId, Card.DrawPile, nil)
        room:doBroadcastNotify("UpdateDrawPile", tostring(#room.draw_pile))
      end
    else
      player:drawCards(self.cost_data, self.name)
    end
  end,
}

local emo__mingdao_tm = fk.CreateTargetModSkill{
  name = "#emo__mingdao_tm",
  bypass_times = function(self, player, skill, scope)
    if player:hasSkill(self) and skill and skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
      local weapon = player:getEquipment(Card.SubtypeWeapon)
      return weapon and Fk:getCardById(weapon).name == "nameless_sword"
    end
  end,
}
emo__mingdao:addRelatedSkill(emo__mingdao_tm)

moe__qianxueji:addSkill(emo__mingdao)

local emo__xinzhuang = fk.CreateTriggerSkill{
  name = "emo__xinzhuang",
  events = {fk.CardUseFinished},
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and data.card.type == Card.TypeEquip 
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local times = player:usedSkillTimes(self.name, Player.HistoryRound)
    return room:askForSkillInvoke(player, self.name, nil, "#emo__xinzhuang-invoke::"..target.id..":"..times+1)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "@emo__xinzhuang-round")
    target:drawCards(3, self.name)
    local times = player:usedSkillTimes(self.name, Player.HistoryRound)
    room:askForDiscard(target, times, times, true, self.name, false)
  end,
}
moe__qianxueji:addSkill(emo__xinzhuang)

local emo__qinyue = fk.CreateActiveSkill{
  name = "emo__qinyue",
  frequency = Skill.Limited,
  anim_type = "support",
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 1,
  target_filter = function(self, to_select, selected)
    local target = Fk:currentRoom():getPlayerById(to_select)
    return #selected == 0 and target.gender ~= Self.gender and not target:isAllNude()
  end,
  prompt = function ()
    return "#emo__qinyue-prompt:::"..Self:usedSkillTimes("emo__xinzhuang", Player.HistoryRound)
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0 and player:usedSkillTimes("emo__xinzhuang", Player.HistoryRound) > 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local x = player:usedSkillTimes("emo__xinzhuang", Player.HistoryRound) 
    for _ = 1, x do
      if player.dead or target:isAllNude() then break end
      local id = room:askForCardChosen(player, target, "hej", self.name)
      local area = room:getCardArea(id)
      room:moveCardTo(id, Card.PlayerHand, player, fk.ReasonPrey, self.name, nil, false, player.id)
      if target.dead then break end
      if area == Card.PlayerEquip and target:isWounded() then
        room:recover { num = 1, skillName = self.name, who = target , recoverBy = player} 
      elseif area == Card.PlayerHand then
        target:drawCards(1, self.name)
      end
    end
  end,
}
moe__qianxueji:addSkill(emo__qinyue)

Fk:loadTranslationTable{
  ["moe__qianxueji"] = "千雪姬",

  ["emo__xinzhuang"] = "新妆",
  [":emo__xinzhuang"] = "一名角色使用装备牌后，你可令其摸三张牌，然后其弃置X张牌（X为本轮〖新妆〗发动次数）。",
  ["#emo__xinzhuang-invoke"] = "新妆：你可令 %dest 摸三张牌，然后其弃 %arg 张牌",
  ["@emo__xinzhuang-round"] = "新妆",
  
  ["emo__mingdao"] = "名刀",
  [":emo__mingdao"] = "锁定技，游戏开始时，将【无名刀】洗入牌库，你装备【无名刀】时使用【杀】无次数限制。每当一张装备牌于你的出牌阶段进入弃牌堆后，你摸一张牌。",

  ["emo__qinyue"] = "亲约",
  [":emo__qinyue"] = "限定技，出牌阶段，你可以依次获得一名其他角色角色区域内X张牌，以此法获得其装备区的一张牌后，令其回复1点体力，获得其一张手牌后，令其摸一张牌（X为本轮〖新妆〗发动次数）。",
  ["#emo__qinyue-prompt"] = "亲约：你可以连续获得一名其他角色角色区域内 %arg 张牌",

  ["$emo__qinyue1"] = "妾发初覆额，折花门前剧",
  ["$emo__qinyue2"] = "同居长干里，两小无嫌猜",
}








local emo__ticong = fk.CreateTriggerSkill{
  name = "emo__ticong",
  frequency = Skill.Wake,
  events = {fk.Death},
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  can_wake = function(self, event, target, player, data) 
    return target.kingdom == player.kingdom and not (data.damage and data.damage.from and data.damage.from == player)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local skills = {}
    player.general = target.general
    room:broadcastProperty(player, "general")
    for _, skill_name in ipairs(Fk.generals[target.general]:getSkillNameList()) do
      local skill = Fk.skills[skill_name]
      if not (skill.lordSkill and player.role ~= "lord") and (#skill.attachedKingdom == 0 or (table.contains(skill.attachedKingdom, player.kingdom))) then
        table.insertIfNeed(skills, skill_name)
      end
    end
    if #skills > 0 then
      room:handleAddLoseSkills(player, table.concat(skills, "|"))
    end
    room:handleAddLoseSkills(player, "-emo__youzhen")
  end,
}
moe__jiao:addSkill(emo__ticong)


Fk:loadTranslationTable{
  ["emo__ticong"] = "替从",
  [":emo__ticong"] = "觉醒技，每当一名其他角色角色死亡时，若其势力与你相同，且伤害来源不为你，你获得其主将武将牌上的技能，并将图像替换为其，然后失去〖游侦〗。",

  ["$emo__ticong1"] = "将军的使命，由我完成",
  ["$emo__ticong2"] = "茫茫草芥，亦慕日月之皓",
}

local moe__chenyutong = General(extension, "moe__chenyutong", "moe", 3, 3, General.Female)

local emo__ranmo = fk.CreateTriggerSkill{
  name = "emo__ranmo",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.TargetConfirmed, fk.CardUsing, fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      if event == fk.TargetConfirmed then
        return data.card.suit == Card.Spade
      elseif event == fk.CardUsing then
        return data.card.suit == Card.Club
      else
        return player:getMark("@@emo__ranmo") ~= 0
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.TurnEnd then
      local room = player.room
      local targets = table.filter(room:getOtherPlayers(player), function (p) return p:getMark("@@emo__ranmo") == 0 end)
      local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, 
      "#emo__ranmo-choose", self.name, true)
      if #tos > 0 then
        self.cost_data = {tos = tos}
        return true
      end
    else
      self.cost_data = nil
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnEnd then
      local to = room:getPlayerById(self.cost_data.tos[1])
      room:setPlayerMark(to, "@@emo__ranmo", 1)
      room:setPlayerMark(player, "@@emo__ranmo", 0)
      room:addTableMark(to, "emo__ranmo_from", player.id)
      player:filterHandcards()
      to:filterHandcards()
    else
      player:drawCards(1, self.name)
    end
  end,

  refresh_events = {fk.BuryVictim, fk.TurnEnd},
  can_refresh = function (self, event, target, player, data)
    if event == fk.BuryVictim then
      return player:getMark("@@emo__ranmo") > 0 and table.contains(player:getTableMark("emo__ranmo_from"), target.id)
    elseif event == fk.TurnEnd then
      return player:getMark("emo__ranmo_from") ~= 0
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.BuryVictim then
      -- 如果赠与者死亡，且没有其他赠与者，移除自己的标记
      room:removeTableMark(player, "emo__ranmo_from", target.id)
      if player:getMark("emo__ranmo_from") == 0 and not player:hasSkill(self) then
        room:setPlayerMark(player, "@@emo__ranmo", 0)
      end
    elseif event == fk.TurnEnd then
      if not player:hasSkill(self) then
        room:setPlayerMark(player, "@@emo__ranmo", 0)
      end
      for _, pid in ipairs(player:getTableMark("emo__ranmo_from")) do
        local from = room:getPlayerById(pid)
        if from:isAlive() and not from:hasSkill(self) then
          room:setPlayerMark(from, "@@emo__ranmo", 1)
          from:filterHandcards()
        end
      end
      room:setPlayerMark(player, "emo__ranmo_from", 0)
    end
    player:filterHandcards()
  end,

  on_acquire = function (self, player)
    player.room:setPlayerMark(player, "@@emo__ranmo", 1)
    player:filterHandcards()
  end,
  on_lose = function (self, player)
    player.room:setPlayerMark(player, "@@emo__ranmo", 0)
    player:filterHandcards()
  end,
}

local emo__ranmo_filter = fk.CreateFilterSkill{
  name = "#emo__ranmo_filter",
  card_filter = function(self, card, player)
    return player:getMark("@@emo__ranmo") > 0 and card.color == Card.Red and table.contains(player.player_cards[Player.Hand], card.id)
  end,
  view_as = function(self, card)
    return Fk:cloneCard(card.name, card.suit == Card.Heart and Card.Spade or Card.Club, card.number)
  end,
}
emo__ranmo:addRelatedSkill(emo__ranmo_filter)

moe__chenyutong:addSkill(emo__ranmo)

local emo__qinshi = fk.CreateTriggerSkill{
  name = "emo__qinshi",
  anim_type = "offensive",
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.color == Card.Black
    and not player:prohibitUse(Fk:cloneCard("slash"))
    and data.tos and table.find(TargetGroup:getRealTargets(data.tos), function (pid)
      local p = player.room:getPlayerById(pid)
      return p ~= player and not p.dead
    end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local slash = Fk:cloneCard("slash")
    slash.skillName = self.name
    local targets = table.filter(TargetGroup:getRealTargets(data.tos), function (pid)
      local p = player.room:getPlayerById(pid)
      return p ~= player and not p.dead and not player:isProhibited(p, slash)
    end)
    local tos = room:askForChoosePlayers(player, targets, 1, 1, "#emo__qinshi-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    room:askForDiscard(player, 2, 2, true, self.name, false, ".|.|.|.|.|basic")
    if not to.dead then
      room:useVirtualCard("slash", nil, player, to, self.name, true)
    end
  end,
}
moe__chenyutong:addSkill(emo__qinshi)

Fk:loadTranslationTable{
  ["moe__chenyutong"] = "陈雨桐",

  ["emo__ranmo"] = "染墨",
  [":emo__ranmo"] = "①你拥有“墨”标记，回合结束时，你可将之交给一名其他角色直到其回合结束；②拥有“墨”标记的角色手牌中的<font color='red'>♥</font>牌视为♠牌，<font color='red'>♦</font>牌视为♣牌；③每当你使用♣牌或成为♠牌的目标后摸一张牌。",
  ["#emo__ranmo-choose"] = "染墨：你可以将“墨”标记交给一名其他角色，直到其回合结束",
  ["@@emo__ranmo"] = "墨",
  ["#emo__ranmo_filter"] = "染墨",

  ["emo__qinshi"] = "侵势",
  [":emo__qinshi"] = "每当你对其他角色使用的黑色牌结算结束后，你可以弃置两张基本牌（无则不弃）并视为对其中一名目标角色使用一张【杀】。",
  ["#emo__qinshi-choose"] = "侵势，选择一名其他角色，弃置两张基本牌并视为对其使用【杀】",
}

local moe__kajiang = General(extension, "moe__kajiang", "moe", 4, 4, General.Female)

local emo__tianze = fk.CreateTriggerSkill{
  name = "emo__tianze",
  events = {fk.EventPhaseStart},
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player.phase == Player.Start and player:hasSkill(self)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local tos = room:askForChoosePlayers(player, table.map(room:getOtherPlayers(player, false), Util.IdMapper),
    1, 1, "#emo__tianze-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    local judge = { who = to, reason = self.name }
    room:judge(judge)
    if to.dead then return end
    if judge.card.type == Card.TypeEquip then
      if not to:hasDelayedTrick("lightning") and not table.contains(to.sealedSlots, Player.JudgeSlot) then
        local cid = judge.card:getEffectiveId()
        if room:getCardArea(cid) == Card.DiscardPile then
          room:useVirtualCard("lightning", cid, player, to, self.name)
        end
      end
    else
      local mark = to:getTableMark("@emo__tianze")
      if table.insertIfNeed(mark, judge.card:getTypeString().."_char") then
        room:setPlayerMark(to, "@emo__tianze", mark)
      end
    end
  end,

  refresh_events = {fk.DamageInflicted},
  can_refresh = function(self, event, target, player, data)
    if target ~= player or not data.card then return false end
    return table.contains(player:getTableMark("@emo__tianze"), data.card:getTypeString().."_char")
  end,
  on_refresh = function(self, event, target, player, data)
    data.damage = data.damage + 1
    player.room:removeTableMark(player, "@emo__tianze", data.card:getTypeString().."_char")
  end,
}

moe__kajiang:addSkill(emo__tianze)

local emo__qiming = fk.CreateTriggerSkill{
  name = "emo__qiming",
  anim_type = "control",
  frequency = Skill.Compulsory,
  events = {fk.AskForRetrial},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and data.card and table.every(player:getCardIds("h"), function (id)
      return Fk:getCardById(id).suit ~= data.card.suit
    end)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if room:getCardArea(data.card) == Card.Processing then
      room:obtainCard(player, data.card, true, fk.ReasonJustMove, player.id, self.name)
    end
    room:retrial(Fk:getCardById(room:getNCards(1)[1]), player, data, self.name)
  end,
}

moe__kajiang:addSkill(emo__qiming)

Fk:loadTranslationTable{
  ["moe__kajiang"] = "佧酱",

  ["emo__tianze"] = "天责",
  [":emo__tianze"] = "准备阶段开始时，你可以令一名其他角色进行判定，其下次受到与判定牌类型相同的牌造成伤害+1，若为装备牌，将此牌当【闪电】对其使用。",
  ["@emo__tianze"] = "天责",
  ["#emo__tianze-choose"] = "天责：令一名其他角色进行一次判定，其下次受到与判定牌类型相同的牌造成伤害+1",

  ["emo__qiming"] = "欺命",
  [":emo__qiming"] = "锁定技，每当一名角色的判定牌生效前，若你没有此花色的手牌，你获得判定牌，并用牌堆顶牌代替之。",
}




local moe__cangwu = General(extension, "moe__cangwu", "moe", 4, 4, General.Female)

local emo__jixing = fk.CreateTriggerSkill{
  name = "emo__jixing",
  events = {fk.EventPhaseStart, fk.TurnEnd},
  anim_type = "control",
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    local x, y = #room.draw_pile, #room.discard_pile
    if player:hasSkill(self) and target == player then
      if y > 2 * x then
        return player:getMark("emo__jixing_extra-round") == 0
      else
        return event == fk.EventPhaseStart and player.phase == Player.Start
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x, y = #room.draw_pile, #room.discard_pile
    if x > 2 * y then
      player:drawCards(1, self.name)
    elseif y > 2 * x then
      room:setPlayerMark(player, "emo__jixing_extra-round", 1)
      player:gainAnExtraTurn(false, self.name)
    else
      local cards = room:getNCards(10)
      room:moveCards({
        ids = cards,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonPutIntoDiscardPile,
      })
    end
  end,
}
moe__cangwu:addSkill(emo__jixing)

local emo__yushan = fk.CreateTriggerSkill{
  name = "emo__yushan",
  anim_type = "offensive",
  events = {fk.AfterCardsMove, fk.DamageCaused, fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return end
    if event == fk.DamageCaused then
      return target == player and player:getMark("@emo__yushan-turn") > 0 and player == player.room.current
    elseif event == fk.DamageInflicted then
      return target == player and player:getMark("@emo__yushan-turn") > 0 and player ~= player.room.current
    elseif player:isKongcheng() and table.find(Fk:currentRoom().alive_players, function (p)
      return p ~= player and not p:isNude()
    end) then
      for _, move in ipairs(data) do
        if move.from == player.id then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DamageCaused then
      self.cost_data = {tos = {data.to.id}}
      return true
    elseif event == fk.DamageInflicted then
      self.cost_data = {}
      return true
    else
      local targets = table.filter(room:getOtherPlayers(player, false), function (p) return not p:isNude() end)
      local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#emo__yushan-throw:::2", self.name, true)
      if #tos > 0 then
        self.cost_data = {tos = tos}
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardsMove then
      room:addPlayerMark(player, "@emo__yushan-turn", 1)
      local num = 2
      local to = room:getPlayerById(self.cost_data.tos[1])
      local cards = room:askForCardsChosen(player, to, 1, 2, "he", self.name)
      room:throwCard(cards, self.name, to, player)
      if #cards ~= 1 or player.dead then return end
      local targets = table.filter(room:getOtherPlayers(player, false), function (p) return not p:isNude() end)
      if #targets == 0 then return false end
      local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#emo__yushan-throw:::1", self.name, true)
      if #tos > 0 then
        to = room:getPlayerById(tos[1])
        cards = room:askForCardsChosen(player, to, 1, 1, "he", self.name)
        room:throwCard(cards, self.name, to, player)
      end
    elseif event == fk.DamageCaused then
      data.damage = data.damage + player:getMark("@emo__yushan-turn")
      room:setPlayerMark(player, "@emo__yushan-turn", 0)
    else
      data.damage = data.damage - player:getMark("@emo__yushan-turn")
      room:setPlayerMark(player, "@emo__yushan-turn", 0)
    end
  end,
}
moe__cangwu:addSkill(emo__yushan)

Fk:loadTranslationTable{
  ["moe__cangwu"] = "苍雾",
  ["#moe__cangwu"] = "雾隐仙子",

  ["emo__jixing"] = "急行",
  [":emo__jixing"] = "锁定技，若X>2Y，准备阶段你摸一张牌，若Y>2X，回合结束后你执行一个额外回合（每轮限一次）。其他情况，准备阶段你将牌堆顶十张牌置入弃牌堆（X为摸牌堆牌数，Y为弃牌堆牌数）。",

  ["emo__yushan"] = "御扇",
  [":emo__yushan"] = "当你失去最后手牌时，你可以弃置其他角色至多两张牌，若为你回合内，本回合你下次造成伤害+1，若为回合外，本回合你下次受到伤害-1。",
  ["#emo__yushan-throw"] = "御扇：你可以弃置其他角色至多 %arg 张牌",
  ["@emo__yushan-turn"] = "御扇",
}

local moe__misheng = General(extension, "moe__misheng", "moe", 4, 4, General.Female)



local emo__zhuojian = fk.CreateTriggerSkill{
  name = "emo__zhuojian",
  anim_type = "control",
  events = {fk.EventPhaseStart, fk.CardUsing},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    if event == fk.EventPhaseStart then
      return target == player and player.phase == Player.Start
    elseif target == player and not player.dead then
      local mark = player:getTableMark("@emo__zhuojian-turn")
      return table.contains(mark, data.card:getTypeString().."_char")
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local mark = {}
      local all_choices = {"basic", "trick", "equip"}
      local choices = table.simpleClone(all_choices)
      for i = 1, 2 do
        local choice = room:askForChoice(player, choices, self.name, "#emo__zhuojian-choice"..i, false, all_choices)
        table.removeOne(choices, choice)
        table.insert(mark, choice.."_char")
      end
      room:setPlayerMark(player, "@emo__zhuojian-turn", mark)
    else
      local mark = player:getTableMark("@emo__zhuojian-turn")
      if data.card:getTypeString().."_char" == mark[1] then
        player:drawCards(1, self.name)
      else
        local targets = room:getOtherPlayers(player, false)
        if #targets == 0 then return false end
        local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#emo__zhuojian-throw", self.name, false)
        if #tos > 0 then
          local to = room:getPlayerById(tos[1])
          if not to:isNude() then
            room:throwCard(table.random(to:getCardIds("he"), 1), self.name, to, player)
          end
        end
      end
    end
  end,

  on_lose = function (self, player)
    player.room:setPlayerMark(player, "@emo__zhuojian-turn", 0)
  end,
}
moe__misheng:addRelatedSkill(emo__zhuojian)




local moe__suofeiya = General(extension, "moe__suofeiya", "moe", 4, 4, General.Female)

local emo__yinglue = fk.CreateActiveSkill{
  name = "emo__yinglue",
  anim_type = "offensive",
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 1,
  prompt = "#emo__yinglue",
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id and Self:canPindian(Fk:currentRoom():getPlayerById(to_select))
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isKongcheng()
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local pindian = player:pindian({target}, self.name)
    local winner = pindian.results[target.id].winner
    local n = math.abs(pindian.fromCard.number - pindian.results[target.id].toCard.number)
    if player == winner then
      if n > 5 then
        U.askForUseVirtualCard(room, player, "slash", nil, self.name, nil, true, true, true, true)
        U.askForUseVirtualCard(room, target, "slash", nil, self.name, nil, true, true, true, true)
      else
        player:addSkillUseHistory(self.name, -1)
      end
    else
      if n > 5 then
        if player.dead then return end
        room:damage { from = target, to = player, damage = 1, skillName = self.name }
      else
        if not player.dead then player:drawCards(2, self.name) end
        if not target.dead then target:drawCards(2, self.name) end
      end
    end
  end,
}

moe__suofeiya:addSkill(emo__yinglue)

local emo__yijian = fk.CreateTriggerSkill{
  name = "emo__yijian$",
  events = {fk.EventPhaseStart},
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill(self) and target.phase == Player.Discard and target.kingdom == "moe"
  end,
  on_cost = function (self, event, target, player, data)
    local choices = {"emo__yijian_maxcard"}
    if not target:isNude() then table.insert(choices, "emo__yijian_recast") end
    table.insert(choices, "Cancel")
    local choice = player.room:askForChoice(target, choices, self.name, "#emo__yijian-invoke:"..player.id)
    if choice ~= "Cancel" then
      self.cost_data = {choice = choice, tos = {target.id}}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = self.cost_data.choice
    if choice == "emo__yijian_maxcard" then
      room:addPlayerMark(target, MarkEnum.AddMaxCards)
      if player:isNude() then return end
      local cards = room:askForCard(player, 1, 1, true, self.name, false, ".", "#emo__yijian-recast")
      if #cards > 0 then
        room:recastCard(cards, player, self.name)
      end
    else
      local cards = room:askForCard(target, 1, 1, true, self.name, false, ".", "#emo__yijian-recast")
      if #cards > 0 then
        room:recastCard(cards, target, self.name)
      end
      room:addPlayerMark(player, MarkEnum.AddMaxCards)
    end
  end,
}

moe__suofeiya:addSkill(emo__yijian)

Fk:loadTranslationTable{
  ["moe__suofeiya"] = "索菲亚",
  
  ["emo__yinglue"] = "英略",
  [":emo__yinglue"] = "出牌阶段限一次，你可以与一名其他角色拼点：若你赢，且点数之差大于5，你与其依次视为使用一张【杀】，若不大于5，视为未发动过〖英略〗；若你没赢，且点数之差大于5，你受到其造成的1点伤害，若不大于5，你与其各摸两张牌。",
  ["#emo__yinglue"] = "与其他角色拼点：若赢且点数差>5，你与其视为使用【杀】；若没赢且点数差>5，你受到伤害",

  ["emo__yijian"] = "易见",
  [":emo__yijian"] = "主公技，其他萌势力角色的弃牌阶段开始时，其可以选一项：1.手牌上限+1；2.重铸一张牌。然后你执行另一项。",
  ["emo__yijian_maxcard"] = "手牌上限+1",
  ["emo__yijian_recast"] = "重铸一张牌",
  ["#emo__yijian-invoke"] = "你可以发动 %src 的“易见”：你选择一项执行，令其执行另一项",
  ["#emo__yijian-recast"] = "易见：重铸一张牌",
}

local moe__tangci = General(extension, "moe__tangci", "moe", 4, 4, General.Female)

local emo__qiaoyu = fk.CreateTriggerSkill{
  name = "emo__qiaoyu",
  anim_type = "control",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target ~= player and not player:isKongcheng() and target == player.room.current
    and data.tos and data.card.type ~= Card.TypeEquip then
      local events = player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
        local use = e.data[1]
        return use and use.from == target.id and use.card.type == data.card.type
      end, Player.HistoryTurn)
      return #events > 0 and events[1].data[1] == data
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local pattern = table.contains(TargetGroup:getRealTargets(data.tos), player.id) and "equip" or "trick"
    local use = U.askForUseRealCard(room, player, nil, ".|.|.|.|.|"..pattern, self.name, "#emo__qiaoyu-"..pattern..":::"..data.card:toLogString(), nil, true)
    if use then
      self.cost_data = use
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local use = self.cost_data
    local current_targets = TargetGroup:getRealTargets(data.tos)
    if table.contains(current_targets, player.id) then
      TargetGroup:removeTarget(data.tos, player.id)
    else
      local to = #current_targets == 1 and current_targets[1] or
      room:askForChoosePlayers(player, current_targets, 1, 1, "#emo__qiaoyu-choose:::"..data.card:toLogString(), self.name, false)[1]
      TargetGroup:removeTarget(data.tos, to)
    end
    room:useCard(use)
  end,
}

moe__tangci:addSkill(emo__qiaoyu)

Fk:loadTranslationTable{
  ["moe__tangci"] = "唐瓷",

  ["emo__qiaoyu"] = "巧御",
  [":emo__qiaoyu"] = "每当一名其他角色于其回合内使用第一张基本牌及第一张锦囊牌时，若目标含有你，你可以使用手牌中一张装备牌，令目标取消你；若目标不含有你，你可以使用手牌中一张锦囊牌，取消一个目标。",
  ["#emo__qiaoyu-equip"] = "巧御：你可使用一张装备牌，令 %arg 目标取消你",
  ["#emo__qiaoyu-trick"] = "巧御：你可使用一张锦囊牌，取消 %arg 的一个目标",
  ["#emo__qiaoyu-choose"] = "巧御：请取消 %arg 的一个目标",
}



return extension
